﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!-- 引入 layui.css -->
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form layui-form-pane" action="">
    <div style="padding: 20px;">
        <h3><i class="layui-icon layui-icon-edit"></i> 编辑工序分类</h3>
    </div>
    <input type="hidden" name="id" id="id" />
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工序分类编号</label>
            <div class="layui-input-block">
                <input type="text" name="processTypeCode" id="processTypeCode" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请输入工序分类编号">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">工序分类名称</label>
            <div class="layui-input-inline">
                <input type="text" name="processTypeName" id="processTypeName" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请输入工序分类名称">
            </div>
        </div>
    </div>
    <div class="layui-form-item" pane>
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="processTypeStatus" value="true" title="启用">
            <input type="radio" name="processTypeStatus" value="false" title="禁用">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="typeRemark" id="typeRemark" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="submitEdit">更新</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var layer = layui.layer;

        // 1) 读取本地存储的id
        var id = localStorage['processTypeId'];
        if (!id) {
            layer.msg('未获取到要编辑的Id', { icon: 2 });
        } else {
            // 2) 加载详情回填
            $.ajax({
                url: 'http://localhost:5141/api/Process/ProcessTypeDetail',
                type: 'GET',
                data: { id: id },
                success: function (res) {
                    if (res && res.code === 200 && res.data) {
                        $('#id').val(res.data.id);
                        $('#processTypeCode').val(res.data.processTypeCode || '');
                        $('#processTypeName').val(res.data.processTypeName || '');
                        $('#typeRemark').val(res.data.typeRemark || '');
                        var status = res.data.processTypeStatus === true ? 'true' : 'false';
                        $("input[name='processTypeStatus'][value='" + status + "']").prop('checked', true);
                        form.render();
                    } else {
                        layer.msg(res && res.message ? res.message : '加载详情失败', { icon: 2 });
                    }
                },
                error: function () { layer.msg('加载详情失败', { icon: 2 }); }
            });
        }

        // 3) 提交事件 - 调用更新接口
        form.on('submit(submitEdit)', function (data) {
            var field = data.field;
            var $btn = $('button[lay-filter="submitEdit"]');
            if ($btn.prop('disabled')) return false;

            if (!field.processTypeCode || !field.processTypeName) {
                layer.msg('请填写所有必填字段', { icon: 2 });
                return false;
            }

            var request = {
                id: parseInt(field.id, 10),
                processTypeCode: field.processTypeCode.trim(),
                processTypeName: field.processTypeName.trim(),
                processTypeStatus: field.processTypeStatus === 'true',
                typeRemark: field.typeRemark ? field.typeRemark.trim() : null
            };

            $btn.prop('disabled', true).text('提交中...');
            $.ajax({
                url: 'http://localhost:5141/api/Process/UpdateProcessType',
                type: 'POST',
                data: JSON.stringify(request),
                contentType: 'application/json',
                success: function (res) {
                    $btn.prop('disabled', false).text('更新');
                    if (res && res.code === 200) {
                        layer.msg('更新成功', { icon: 1, time: 1200 }, function () {
                            if (window.parent && window.parent.loadPage) {
                                window.parent.loadPage('/Process/ProcessTypeList', '工序分类管理');
                            }
                        });
                    } else {
                        layer.msg(res && res.message ? res.message : '更新失败', { icon: 2 });
                    }
                },
                error: function () {
                    $btn.prop('disabled', false).text('更新');
                    layer.msg('网络错误，请重试', { icon: 2 });
                }
            });
            return false;
        });
    });
</script>


